<template>
    <div class="container-fluid">

        <div class="row">
            <div class="col-lg-12">
                <div class="card">

                    <div class="card-toolbar clearfix">
                        <form class="form-inline" onsubmit="return false;">
                            <div class="form-group" style="margin-right:100px;">
                                <a class="btn btn-primary m-r-5" href="javascript:void(0)"
                                   @click="openDialogFormVisible"><i class="mdi mdi-plus"></i>添加敏感词</a>
                            </div>

                            <div class="form-group">
                                <el-select v-model="level" placeholder="全部">
                                    <el-option
                                            key=""
                                            label="全部"
                                            value="">
                                    </el-option>
                                    <el-option
                                            v-for="item in selectOptions"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                            </div>

                            <div class="form-group">
                                <input class="form-control" type="text" id="example-if-email"
                                       name="example-if-email" placeholder="敏感词关键字" v-model="keyword"/>
                            </div>
                            <div class="form-group">
                                <button class="btn btn-success" type="submit" v-on:click="search()">筛选</button>
                            </div>
                        </form>
                    </div>
                    <div class="card-body" v-loading="loading">
                        <div class="table-responsive" style="width: 100%;margin-top: 20px;">
                            <el-table
                                    :data="list"
                                    style="width: 100%"
                                    @sort-change='sortChange'
                                    :default-sort="{prop: '_id', order: 'descending'}"
                            >
                                <el-table-column
                                        type="index"
                                        :index="indexMethod"
                                        label="ID"
                                        align="center"
                                        width="40">
                                </el-table-column>
                                <el-table-column
                                        prop="name"
                                        label="敏感词"
                                        align="center"
                                        width="60">
                                </el-table-column>
                                <el-table-column
                                        prop="title"
                                        label="级别"
                                        align="center"
                                        width="180">
                                    <template slot-scope="scope">
                                        <a @click="handleEdit(scope.row)" style="cursor:pointer;">
                                            {{scope.row.levelName}}
                                        </a>
                                    </template>
                                </el-table-column>
                                <el-table-column
                                        prop="created_at"
                                        label="发布日期"
                                        align="center"
                                        width="180">
                                </el-table-column>
                                <el-table-column label="操作" align="center">
                                    <template slot-scope="scope">

                                        <el-button
                                                size="mini"
                                                @click="handleEdit(scope.row)">设置
                                        </el-button>
                                        <el-button
                                                size="mini"
                                                type="danger"
                                                @click="handleDelete(scope.row)">删除
                                        </el-button>
                                    </template>
                                </el-table-column>
                            </el-table>

                            <nav style="margin-top: 20px">
                                <el-pagination
                                        layout="total, sizes, prev, pager, next"
                                        :total="total"
                                        :hide-on-single-page="pageActive"
                                        @size-change="handleSizeChange"
                                        :page-size="pageSize"
                                        :current-page="currentPage"
                                        :page-sizes="[10, 20, 30, 40]"
                                        @current-change="handleCurrentChange"
                                >
                                </el-pagination>
                            </nav>


                            <el-dialog title="添加敏感词" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
                                <el-form :model="form">

                                    <el-form-item label="敏感词级别">
                                        <el-select v-model="form.level" placeholder="全部">
                                            <el-option
                                                    v-for="item in selectOptions"
                                                    :key="item.value"
                                                    :label="item.label"
                                                    :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>


                                    <el-form-item label="敏感词" :label-width="formLabelWidth" v-if="showBody">
                                        <el-input
                                                type="textarea"
                                                placeholder="可以批量添加，一行一个敏感词"
                                                v-model="form.body"
                                                maxlength="100"
                                                :rows="4"
                                                show-word-limit
                                        >
                                        </el-input>
                                    </el-form-item>


                                </el-form>
                                <div slot="footer" class="dialog-footer">
                                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                                    <el-button type="primary" @click="submit">确 定</el-button>
                                </div>
                            </el-dialog>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "SensitiveWordComponentComponent",
        data() {
            return {
                keyword: '',
                pageActive: true,
                list: [],
                pageSize: 10,
                total: 0,
                currentPage: 1,
                loading: true,
                level: "",

                selectOptions: [],
                form: {
                    level: 1,
                    name: '',
                    body:''
                },
                dialogFormVisible: false,
                formLabelWidth: '82px',
                id: "",
                order:'desc',
                orderField:'_id',
            }
        },
        methods: {
            search: function () {
                let self = this;
                let uri = 'sensitive-word/post-list';
                self.loading = true;

                let postData = {
                    keyword: self.keyword,
                    page: self.currentPage,
                    limit: self.pageSize,
                    orderField: self.orderField,
                    order: self.order,
                    catId:self.catId
                };
                axios.post(uri,postData).then(function (result) {

                    self.total = result.data.total;
                    self.list = result.data.data;
                    console.log(result.data.data);
                   /* if (self.total > 0) {
                        self.pageActive = false;
                    } else {
                        self.pageActive = true;
                    }
                    self.list = result.data.data;*/
                    self.loading = false;
                }).catch(function (error) {
                    self.loading = false;
                });
            },
            indexMethod(index) {
                return index + 1;
            },
            openDialogFormVisible() {
                this.dialogFormVisible = true;
                this.form.level = 1;
                this.form.name = '';
                this.form.body = '';
                this.showBody = true;
                this.id = "";
            },
            handleCurrentChange: function (val) {
                this.currentPage = val;
                this.search();
            },
            handleSizeChange: function (pageSize) {
                this.pageSize = pageSize;
                this.search();
            },
            submit() {
                let self = this;
                let uri = "sensitive-word/store";
                if (!self.form.level) {
                    self.$message.error("必须填写“级别”");
                    return;
                }
                if (!self.form.body && !self.id) {
                    self.$message.error("必须添加敏感词");
                    return;
                }
                self.loading = true;
                let postData = this.form;
                if (!self.id) {
                    self.store(uri,postData);
                } else {
                    uri = "sensitive-word/"+self.id;
                    self.update(uri,postData);
                }
            },
            store: function (uri, postData) { //保存
                let self = this;
                axios.post(uri,postData).then(function (result) {
                    self.dialogFormVisible = false;
                    self.search();
                }).catch(function (error) {
                    self.loading = false;
                });
            },
            update: function (uri,postData) { //更新数据
                let self = this;
                axios.put(uri,postData).then(function (result) {
                    self.dialogFormVisible = false;
                    self.search();
                }).catch(function (error) {
                    self.loading = false;
                });
            },
            sortChange(item) {
                let order = "asc";
                this.orderField = item.prop;
                console.log(item);
                if (item.order === 'descending') {
                    order = "desc";
                }
                this.order = order;
                this.search();
            },
            handleDelete:function (row) {
                let self = this;
                let tipStr = "此操作将永久删除"+row.name+", 是否继续?";
                self.$confirm(tipStr, '敏感词删除', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    self.deleteRequire(row._id)
                }).catch(() => {
                    self.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            deleteRequire:function (id) {
                let self = this;
                let uri = "sensitive-word/" + id;

                axios.delete(uri).then(function (result) {
                    self.search();
                }).catch(function (error) {
                    self.loading = false;
                });
            },
            handleEdit:function (row) {
                this.dialogFormVisible = true;
                this.form.level = row.level;
                this.id = row._id;
            },
            getConfig:function () {
                let self = this;
                self.loading = true;
                axios.get('sensitive-word/config').then(function(result){
                    let sensitiveWordLevels = result.data.sensitiveWordLevels;
                    self.selectOptions = [];
                    sensitiveWordLevels.forEach((item) => {
                        self.selectOptions.push({value: item.id, label: item.level});
                    });
                    self.loading = false;
                }).catch(function (error) {
                    self.loading = false;
                });
            }
        },
        mounted: function () {
            this.search();
            this.getConfig();
        },
        filters: {
            titleFormatter (title) {

                if (title.length >= 10) {
                    return title.substring(0, 10) + '...';
                }
                return title.length === 0 ? '--': title;
                //return this.filterLength(title,10);
            }
        }
    }
</script>

<style scoped>

</style>
